package action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import service.IProductService;
import vo.Product;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@SuppressWarnings("serial")
public class ProductAction extends ActionSupport implements ModelDriven<Product>{
	private int page;
	private int rows;
	private Float ppriceLow;
	private Float ppriceUp;
	private IProductService productService;
	public Product product=new Product();
	
	
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public Float getPpriceLow() {
		return ppriceLow;
	}
	public void setPpriceLow(Float ppriceLow) {
		this.ppriceLow = ppriceLow;
	}
	public Float getPpriceUp() {
		return ppriceUp;
	}
	public void setPpriceUp(Float ppriceUp) {
		this.ppriceUp = ppriceUp;
	}
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	
	//=================前台返回Json对象============================
		
		//查找所有的学生信息并分页
	
		//========================================================
	
    
	
	
	public IProductService getProductService() {
		return productService;
	}
	public void setProductService(IProductService productService) {
		this.productService = productService;
	}
	
   //====================1.显示所有商品=============================== 
    public String showProduct() throws Exception{
    	String hql=createHql();
    	List product1=productService.getProduct(page, rows,hql);
    	int total=productService.getAllProduct(hql);
    	
    	Map<String,Object>  map=new HashMap<String,Object>();
    	map.put("total", total);
    	map.put("rows", product1);
    	writeJson(map);  
    	
    	 product=new Product();
    	 ppriceUp=null;
	        ppriceLow=null;
    	
    	return null;
    	
    }
    
	
	//===================2.添加商品==========================================
    public String addProduct(){
    	
    	
      Result result = new Result();
        try {
        	productService.addProduct(product);
            result.setSuccess(true);
            result.setMsg("添加商品成功");
          
        } catch (Exception e) {
            result.setMsg("添加商品失败");
        }
        writeJson(result);
        product=new Product();
		
   	
   	return null;
    	
    }
    
	
	//======3.删除商品============
    public String delProduct(){
    	
    	
    	 Result result = new Result();
         try {
        	 productService.delProduct(product.getProductId());
             result.setSuccess(true);
             result.setMsg("成功删除一条记录");
           
         } catch (Exception e) {
             result.setMsg("删除失败");
         }
         writeJson(result);
         product=new Product();
		
    	
    	return null;
    	
    }
	
	//======4.编辑商品============
    public void editProduct(){
		
	    	
	      Result result = new Result();
	        try {
	        	 productService.editProduct(product);
	            result.setSuccess(true);
	            result.setMsg("成功修改一条商品记录");
	          
	        } catch (Exception e) {
	            result.setMsg("删除失败");
	        }
	        writeJson(result);
	        product=new Product();
	        
			
	   	
	   
	    	
		
	}
	
	//======5.查询商品============
	
	//======6.获得所有的product============
    //========7.1向前台输出json的writeJson方法====================
    public void writeJson(Object object) {
        try {         
        	JSONObject jsonObjectFromMap = JSONObject.fromObject(object);
        
        	String out=jsonObjectFromMap.toString();
        	System.out.println(out);
          
            ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
            ServletActionContext.getResponse().getWriter().write(out);
            ServletActionContext.getResponse().getWriter().flush();
        } catch (IOException e) {
        	//productService.delProduct(product.getProductId());
            }
     
    }
    //========7.2获得查询的hql语句====================
    public String createHql(){
    	
    	
    	
    	
    	String hql="from Product p where 1=1 ";
    
    	
    	
		
		System.out.println("3 "+hql);
			
			if(product.getProductId()!=null)
				hql+=" and p.productId="+product.getProductId();
			System.out.println("4"+hql);

			if(product.getProductName()!=null)
				hql+=" and p.productName like '%"+product.getProductName()+"%'";
			if(product.getPclassName()!=null)
				hql+=" and p.pclassName like '%"+product.getPclassName()+"%'";
			
			System.out.println("6"+hql);
			if(ppriceLow!=null)
				hql+=" and p.pprice >="+ppriceLow;
			System.out.println("1"+hql);
			if(ppriceUp!=null)
				hql+=" and p.pprice <="+ppriceUp;
			System.out.println("2"+hql);
			
			// product=new Product();
			
		
    	return hql;
    	
    }
	public Product getModel() {
		
		return product;
	}
	
    
}
